package club.banyuan.reserve.controller.api;

import club.banyuan.reserve.common.CommonResult;
import club.banyuan.reserve.dto.BookedParam;
import club.banyuan.reserve.model.*;
import club.banyuan.reserve.service.BookService;
import club.banyuan.reserve.vo.PositionRecordsVo;
import club.banyuan.reserve.vo.PositionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author HanChao
 * 描述信息：
 */
@RestController
@RequestMapping(value = "/api/book")
public class BookController {

    @Autowired
    private BookService bookService;

    @GetMapping(value = "/list")
    public CommonResult seatShow () {

        List<PositionVo> list = bookService.showPositionList();

        return CommonResult.success(list);
    }

    @GetMapping(value = "/timelist/{id}")
    public CommonResult bookShow (@PathVariable(value = "id") Integer positionId) {

        PositionRecordsVo positionRecordsVo = bookService.showBookTimeList(positionId);

        return CommonResult.success(positionRecordsVo);
    }

    @PostMapping(value = "/booking")
    public CommonResult booking (@RequestBody List<BookedParam> params,
                                 HttpSession session) {

        // 查看当前用户
        User user = (User) session.getAttribute("user");

        if (user == null) {
            return CommonResult.failed("请登录");
        }

        bookService.booking(params,user.getId());

        return CommonResult.success("OK");
    }

    @PostMapping(value = "/cancel/{id}")
    public CommonResult cancel (@PathVariable(value = "id") Integer orderId,
                                HttpSession session) {

        User user = (User) session.getAttribute("user");
        if (user == null) {
            return CommonResult.failed("no");
        }

        bookService.bookCancelByOrderId(orderId);

        return CommonResult.success("OK");
    }
}
